package com.bio.action.user;

import java.util.Calendar;
import java.util.List;

import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.bio.action.AbstractAction;
import com.bio.entity.user.Usertype;
import com.bio.service.user.UsertypeService;
import com.bio.utils.Environment;

@Namespace("/usertype")
@Results( {
		@Result(name = "list", location = "/WEB-INF/views/user/listUsertypes.jsp", type = "dispatcher"),
		@Result(name = "tolist", location = "usertype!list", type = "redirect"),
		@Result(name = "edit", location = "/WEB-INF/views/user/editUsertype.jsp", type = "dispatcher"),
		@Result(name = "add", location = "/WEB-INF/views/user/addUsertype.jsp", type = "dispatcher")
		})
public class UsertypeAction extends AbstractAction<Usertype, UsertypeService> {

	private static final long serialVersionUID = -2819569391562442701L;
	
	public UsertypeAction(UsertypeService service) {
		super(service);
	}
	
	Logger logger = Logger.getLogger(UsertypeAction.class);
	
	private Usertype usertype;
	
	private String json;
	
	private List<Usertype> allUsertypes;
	
	private Long companyId;
	
	public String list(){
		companyId = Environment.Session.getCompanyId();
		pagination.setCurrentIndex(this.getCurrentIndex());
		this.pager = this.service.findPagerByCompanyId(pagination, companyId);
		return "list";
	}
	
	public String add(){
		return "add";
	}
	
	public String save(){
		if(null != service.getByNameAndCompany(usertype.getName(), Environment.Session.getCompanyId(), null)){
			this.message = "此类型已存在";
			return "add";
		}
		
		Calendar now = Calendar.getInstance();
		this.usertype.setCreatedDate(now);
		this.usertype.setCompanyId(Environment.Session.getCompanyId());
		
		this.service.save(usertype);
		return "tolist";
	}

	public String edit(){
		this.usertype = this.service.get(id);
		return "edit";
	}
	
	public String update(){
		if(null != service.getByNameAndCompany(usertype.getName(), Environment.Session.getCompanyId(), usertype.getId())){
			this.message = "此类型已存在";
			return "edit";
		}
		
		Usertype old = this.service.get(usertype.getId());
		this.usertype.setCreatedDate(old.getCreatedDate());
		this.usertype.setUpdatedDate(Calendar.getInstance());
		
		this.service.save(usertype);
		return "tolist";
	}
	
	public Usertype getUsertype() {
		return usertype;
	}

	public void setUsertype(Usertype usertype) {
		this.usertype = usertype;
	}

	public String getJson() {
		return json;
	}

	public void setJson(String json) {
		this.json = json;
	}

	public List<Usertype> getAllUsertypes() {
		return allUsertypes;
	}

	public void setAllUsertypes(List<Usertype> allUsertypes) {
		this.allUsertypes = allUsertypes;
	}

	public Long getCompanyId() {
		return companyId;
	}

	public void setCompanyId(Long companyId) {
		this.companyId = companyId;
	}
	
}
